'use strict';
app.controller('wechatConfigCtrl',['$scope','utilService','$modal','$log', function($scope,utilService,$modal,$log) {
    $scope.shop = {};
    // call server.
    console.info($scope)
    $scope.query = function () {
        console.info($scope.shop);
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'wechat/config',
            params: $scope.shop
        }).then(function (result) {
            console.info(result);
        });
    }
    $scope.query();

    $scope.openDelete = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'deleteWechatConfig.html',
            controller: 'deleteWechatConfigCtrl',
            scope:$scope,
            size:'sm',
            resolve: {
                data:function(){
                    var data={};
                    data.id=id;
                    data.name=name;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });

    };
}]);

app.controller('wechatConfigAddCtrl',['$scope','wechatConfigService','$stateParams','toaster','$state',
    function($scope,wechatConfigService,$stateParams,toaster,$state) {

        wechatConfigService.getShopName($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.data = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

        wechatConfigService.getChainName($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.chainData = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

        $scope.wechatConfigAdd = function(){
            wechatConfigService.wechatConfigAdd($scope).then(function(result){
                console.info(result);
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $state.go("app.wechatConfig");
                        break;
                    case 644:
                        toaster.pop('error', "操作失败", "该店铺已存在");
                        break;
                    case 635:
                        toaster.pop('error', "操作失败", "该AppID已存在");
                        break;
                    case 636:
                        toaster.pop('error', "操作失败", "该Secret已存在");
                        break;
                    case 637:
                        toaster.pop('error', "操作失败", "该Partner已存在");
                        break;
                    case 638:
                        toaster.pop('error', "操作失败", "该Secret For Pay已存在");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
}]);


app.controller('wechatConfigViewCtrl',['$scope','wechatConfigService','$stateParams','toaster','$state',
    function($scope,wechatConfigService,$stateParams,toaster,$state) {
        $scope.id = $stateParams.id;
        wechatConfigService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.wechat = result.data;
                    if(result.data.isSys == 1){
                        $scope.wechat.isSys = true;
                    }else{
                        $scope.wechat.isSys = false;
                    }
                    console.info($scope.wechat)
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });

        wechatConfigService.getShopName($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.data = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });

        wechatConfigService.getChainName($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.chainData = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

        $scope.editWechatConfig = function(){
            if($scope.wechat.isSys == true){
                $scope.wechat.isSys = 1;
            }else{
                $scope.wechat.isSys = 0;
            }
            wechatConfigService.updateWechatConfig($scope).then(function(result){
                console.info(result);
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $state.go("app.wechatConfig");
                        break;
                    case 644:
                        toaster.pop('error', "操作失败", "该店铺已存在");
                        break;
                    case 635:
                        toaster.pop('error', "操作失败", "该AppID已存在");
                        break;
                    case 636:
                        toaster.pop('error', "操作失败", "该Secret已存在");
                        break;
                    case 637:
                        toaster.pop('error', "操作失败", "该Partner已存在");
                        break;
                    case 638:
                        toaster.pop('error', "操作失败", "该Secret For Pay已存在");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }
}]);

//删除模态框Controller
app.controller('deleteWechatConfigCtrl', ['$scope', '$modalInstance','$state','wechatConfigService','toaster','data',
    function($scope, $modalInstance,$state,wechatConfigService,toaster,data) {
        $scope.data=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }

            $scope.AllowToDelete=false;

            var id=$scope.data.id;
            wechatConfigService.deleteWechatConfig($scope,id).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $state.reload("app.wechatConfig");
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

    }])
;